package cc.linker.steplink.crm.merchanth5.service;

import cc.linker.steplink.crm.merchanth5.dao.VipExchangeDao;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@EnableAsync
@Service
public class VipExchangeCodeService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private VipExchangeDao vipExchangeDao;
    /**
     * 异步生成兑换码
     * @param param
     * @param exchangeType
     * @param orgId
     */
    @Async
    public void addCode(Map param,Integer exchangeType,String exchangeId,Integer num){
        stringRedisTemplate.opsForValue().set("exchangeCode:exchangeCodeDoing_"+exchangeId,"1",30, TimeUnit.MINUTES);
        if(exchangeType==1){
            if(StringUtils.isEmpty(param.get("exchangeNum"))){
                throw GlobalException.build("参数错误");
            }
            Integer exchangeNum = Integer.valueOf(param.get("exchangeNum").toString());
            if(exchangeNum==0){
                throw GlobalException.build("兑换人数需要大于0");
            }
            List<Map<String,Object>> exchangeCodesList = new ArrayList();
            for(int i=0;i<exchangeNum;i++){
                Map map = new HashMap();
                String exchangeCode = getNumRandom(num);
                exchangeCode = exchangeId + exchangeCode;
                Long exCount = stringRedisTemplate.opsForSet().add("exchangeCode:exchangeCode_"+exchangeId,exchangeCode);
                while (exCount<=0){
                    exchangeCode = getNumRandom(num);
                    exchangeCode = exchangeId + exchangeCode;
                    exCount = stringRedisTemplate.opsForSet().add("exchangeCode:exchangeCode_"+exchangeId,exchangeCode);
                }
                map.put("exchangeCode",exchangeCode);
                map.put("exchangeId",param.get("exchangeId"));
                map.put("exchangeDays",param.get("exchangeDays"));
                exchangeCodesList.add(map);
            }
            if(exchangeCodesList.size()<5001){
                vipExchangeDao.saveExchangeCode(exchangeCodesList);
            }else {
                List<List<Map<String,Object>>> aa= split(exchangeCodesList,5000);
                if(aa!=null && aa.size()>0){
                    for(List<Map<String,Object>> bb : aa){
                        vipExchangeDao.saveExchangeCode(bb);
                    }
                }
            }

        }else {
            if(StringUtils.isEmpty(param.get("exchangeCodesList"))){
                throw GlobalException.build("参数错误");
            }
            List<Map<String,Object>> exchangeCodesList =(List<Map<String,Object>>) param.get("exchangeCodesList");
            for(Map map : exchangeCodesList){
                String exchangeCode = getNumRandom(num);
                exchangeCode = exchangeId + exchangeCode;
                Long exCount = stringRedisTemplate.opsForSet().add("exchangeCode:exchangeCode"+exchangeId,exchangeCode);
                while (exCount<=0){
                    exchangeCode = getNumRandom(num);
                    exchangeCode = exchangeId + exchangeCode;
                    exCount = stringRedisTemplate.opsForSet().add("exchangeCode:exchangeCode"+exchangeId,exchangeCode);
                }
                map.put("exchangeCode",exchangeCode);
                map.put("exchangeId",param.get("exchangeId"));
            }
            if(exchangeCodesList.size()<5001){
                vipExchangeDao.saveExchangeCode(exchangeCodesList);
            }else {
                List<List<Map<String,Object>>> aa= split(exchangeCodesList,5000);
                if(aa!=null && aa.size()>0){
                    for(List<Map<String,Object>> bb : aa){
                        vipExchangeDao.saveExchangeCode(bb);
                    }
                }
            }
        }
        stringRedisTemplate.delete("exchangeCode:exchangeCodeDoing_"+exchangeId);
    }

    public static String getNumRandom(int count){
        int first = new Random().nextInt(9);
        int rnd = UUID.randomUUID().toString().hashCode();
        if(rnd < 0){
            rnd = -rnd;
        }
        return first+String.format("%0"+(count-1)+"d", rnd);
    }



    /**
     * 拆分集合
     * 用于批处理，在批量操作数据库时候数据太大的时候最好还是先拆分
     *
     * @param <T>
     * @param resList 要拆分的集合
     * @param count   每个集合的元素个数
     * @return 返回拆分后的各个集合
     */
    public static <T> List<List<T>> split(List<T> resList, int count) {
        if (resList == null || count < 1) {
            return null;
        }
        List<List<T>> ret = new ArrayList<List<T>>();
        int size = resList.size();
        if (size <= count) { //数据量不足count指定的大小
            ret.add(resList);
        } else {
            int pre = size / count;
            int last = size % count;
            //前面pre个集合，每个大小都是count个元素
            for (int i = 0; i < pre; i++) {
                List<T> itemList = new ArrayList<T>();
                for (int j = 0; j < count; j++) {
                    itemList.add(resList.get(i * count + j));
                }
                ret.add(itemList);
            }
            //last的进行处理
            if (last > 0) {
                List<T> itemList = new ArrayList<T>();
                for (int i = 0; i < last; i++) {
                    itemList.add(resList.get(pre * count + i));
                }
                ret.add(itemList);
            }
        }
        return ret;
    }
}
